home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / Imageer 1.0.0d3 / source / windows.c < prev   
Encoding:
C/C++ Source or Header  |  1996-02-16  |  20.4 KB  |  697 lines  |  [TEXT/MPS ]

  1. /****************************************************/
  2. /*                                                    */
  3. /*    File:        windows.c                            */
  4. /*                                                    */
  5. /*    Program:    Imageer                                */
  6. /*                                                    */
  7. /*    By:            Jason Hodges-Harris                    */
  8. /*                                                    */
  9. /*    Created:    26/10/95  00:00:00 AM                */
  10. /*                                                    */
  11. /*    Version:    1.0.0d3                                */
  12. /*                                                    */
  13. /*    Copyright:    © 1995-96 Apple Computer, Inc.,        */ 
  14. /*                    all rights reserved.            */        
  15. /*                                                    */
  16. /****************************************************/
  17.  
  18.  
  19. /**** Macintosh Toolbox Headers *****/
  20.  
  21. #ifndef __CONTROLS__
  22. #include <Controls.h>
  23. #endif
  24.  
  25. #ifndef __DEVICES__
  26. #include <Devices.h>
  27. #endif
  28.  
  29. #ifndef __DIALOGS__
  30. #include <Dialogs.h>
  31. #endif
  32.  
  33. #ifndef __GXENVIRONMENT__
  34. #include <GXEnvironment.h>
  35. #endif
  36.  
  37. #ifndef __GXGRAPHICS__
  38. #include <GXGraphics.h>
  39. #endif
  40.  
  41. #ifndef __LOWMEM__
  42. #include<LowMem.h>
  43. #endif
  44.  
  45. #ifndef __PALETTES__
  46. #include <Palettes.h>
  47. #endif
  48.  
  49. #ifndef __QDOFFSCREEN__
  50. #include <QDOffscreen.h>
  51. #endif
  52.  
  53. #ifndef __TEXTUTILS__
  54. #include <TextUtils.h>
  55. #endif
  56.  
  57. #ifndef __TOOLUTILS__
  58. #include <ToolUtils.h>
  59. #endif
  60.  
  61. #ifndef __WINDOWS__
  62. #include <Windows.h>
  63. #endif
  64.  
  65.  
  66. /****   Application headers and prototypes   ****/
  67.  
  68.  
  69. #ifndef __IMAGEERAPPHEADER__
  70. #include "Imageer.app.h"
  71. #endif
  72.  
  73. #ifndef __IMAGEERPROTOSHEADER__
  74. #include "Imageer.protos.h"
  75. #endif
  76.  
  77.  
  78. /***** Global Variables *****/
  79.  
  80. extern short            gNumOpenWindows;        //    number of open document windows
  81.  
  82.  
  83.  /* The DisplayAlert() function is a generic modal dialog display function
  84.     which uses GetStdFilterProc() and SetDialogDefaultItem() to
  85.     automatically get the standard filter and outline the default DITL item.
  86.     These 'undocumented' functions are available in system 7.0 or later. */
  87.  
  88. #pragma segment windows
  89. long    DisplayAlert(short dialogID,short errStrID,short StrIDindex)
  90. {
  91.     GrafPtr            savePort = nil;
  92.     DialogPtr        myDialog = nil;
  93.     ModalFilterUPP    theFilter = nil;
  94.     Str255            theTextStr;
  95.     short            alertResponse;    
  96.  
  97.     myDialog = GetNewDialog(dialogID,nil,((WindowPtr)-1L));    // get dialog resource
  98.     GetPort(&savePort);
  99.     SetPort    (myDialog);
  100.     if (GetStdFilterProc(&theFilter) !=noErr)
  101.         DebugStr("\pFailed to get standard dialog filter.");
  102.     SetDialogDefaultItem(myDialog,1);
  103.     /* If the errStrID variable contains 0, the dialog doesn't
  104.        require any text from a STR# resource */
  105.     if (errStrID!=0)        // get STR# resource text
  106.     {
  107.         GetIndString(theTextStr,errStrID,StrIDindex);
  108.         ParamText(theTextStr,(ConstStr255Param)kNull_Str,
  109.                   (ConstStr255Param)kNull_Str,(ConstStr255Param)kNull_Str);
  110.     }
  111.     do{
  112.         ModalDialog(theFilter,&alertResponse);
  113.     } while (alertResponse==0);
  114.     DisposeDialog(myDialog);//    dispose dialog mem allocation
  115.     SetPort(savePort);        
  116.     return alertResponse;    // return dialog return value
  117. }
  118.  
  119.  
  120. // Drag the window from the position passed in the mouseLoc parameter 
  121.  
  122. #pragma segment windows
  123. void DragSelWind(WindowPtr window,Point mouseLoc)
  124. {
  125.     Rect    dragBounds;
  126.     
  127.     dragBounds=(**GetGrayRgn()).rgnBBox;
  128.     DragWindow(window,mouseLoc,&dragBounds);
  129. }
  130.  
  131.  
  132. // Close the active window which had it's close box selected
  133. #pragma segment windows
  134. void DoGoAwayWind(WindowPtr window,Point mouseLoc)
  135. {
  136.     if(TrackGoAway(window,mouseLoc))
  137.     {
  138.         if (window!=nil)
  139.         {
  140.             // Test for a dialog window and hide if selected
  141.             if ((((WindowPeek)window)->windowKind)==2) 
  142.                 HideWindow(window);
  143.             // Test for a Desk Accessory window and Close.
  144.             else if ((((WindowPeek)window)->windowKind)<0) 
  145.                 CloseDeskAcc(((WindowPeek)window)->windowKind);
  146.             // Test for an application window.
  147.             else if ((((WindowPeek)window)->windowKind)==8)    
  148.             {
  149.                 // Handle application windows
  150.                 DisposeImageWindow (window);
  151.             }
  152.         }
  153.     }
  154. }
  155.  
  156.  
  157. /**** create new image window    ****/
  158.  
  159. // Create a new window complete with scroll bar controls
  160. // position staggered at max possible size and resize to
  161. // be completely visible on the main screen.
  162.  
  163. #pragma segment windows
  164. OSErr    CreateImageWindow (ImageDocHndl theWindDocHndl)
  165. {
  166.     ImageDocHndl    theWindowRefcon = nil;
  167.     PaletteHandle    theWPalette = nil;
  168.     CWindowPtr        theCWindow;
  169.     Point            theWindowOffset;
  170.     short            maxXsize,
  171.                     maxYsize;
  172.     OSErr            error = noErr;
  173.                     
  174.     theCWindow = (CWindowPtr)GetNewCWindow(rGenWindow,nil,(WindowPtr)-1L);
  175.     SetWRefCon((WindowPtr)theCWindow,(long)theWindDocHndl);    // set window refcon to doc handle
  176.     SetWTitle((WindowPtr)theCWindow,(**theWindDocHndl).theImageFileReply.sfFile.name);
  177.     CalcMaxImageWindowSize(theWindDocHndl);
  178.     theWindowOffset    = CalcNextWindowPosition(theWindDocHndl);
  179.     SizeWindow((WindowPtr)theCWindow,
  180.         (**theWindDocHndl).theMaxWindowSize.right - (**theWindDocHndl).theMaxWindowSize.left,
  181.         (**theWindDocHndl).theMaxWindowSize.bottom - (**theWindDocHndl).theMaxWindowSize.top,false);
  182.     MoveWindow((WindowPtr)theCWindow,theWindowOffset.h,theWindowOffset.v,false);
  183.     (**theWindDocHndl).theVScrollBar = GetNewControl (rVScrollBar,(WindowPtr)theCWindow);
  184.     (**theWindDocHndl).theHScrollBar = GetNewControl (rHScrollBar,(WindowPtr)theCWindow);
  185.     (**theWindDocHndl).theColorsPalette = nil;    // not used by image window
  186.     (**theWindDocHndl).isColorsWindow = false;
  187.     if ((**theWindDocHndl).theVScrollBar == nil || (**theWindDocHndl).theHScrollBar == nil)
  188.     {
  189.         DisposeWindow((WindowPtr)theCWindow);
  190.         return kDefaultAppError;
  191.     }
  192.  
  193.     MoveControl((**theWindDocHndl).theVScrollBar,
  194.                 theCWindow->portRect.right - kScrollBarWidth,theCWindow->portRect.top - 1);
  195.     MoveControl((**theWindDocHndl).theHScrollBar,
  196.                 theCWindow->portRect.left - 1,theCWindow->portRect.bottom - kScrollBarWidth);
  197.     SizeControl((**theWindDocHndl).theVScrollBar,16,
  198.                 (theCWindow->portRect.bottom - theCWindow->portRect.top)-13);
  199.     SizeControl((**theWindDocHndl).theHScrollBar,
  200.                 (theCWindow->portRect.right - theCWindow->portRect.left)-13,16);
  201.     theWPalette = 
  202.         NewPalette(256,(**(*(**theWindDocHndl).theImageWorld).portPixMap).pmTable,pmTolerant+pmExplicit,0);
  203.     NSetPalette((WindowPtr)theCWindow,theWPalette,pmAllUpdates);
  204.     ShowControl((**theWindDocHndl).theVScrollBar);
  205.     ShowControl((**theWindDocHndl).theHScrollBar);
  206.     SetControlMinimum((**theWindDocHndl).theVScrollBar,0);
  207.     SetControlMinimum((**theWindDocHndl).theHScrollBar,0);
  208.     maxXsize = (*theWindDocHndl)->theImageWorld->portRect.right - 
  209.                 ((theCWindow->portRect.right - kScrollBarWidth) - theCWindow->portRect.left);
  210.     maxYsize = (*theWindDocHndl)->theImageWorld->portRect.bottom - 
  211.                 ((theCWindow->portRect.bottom - kScrollBarWidth) - theCWindow->portRect.top);
  212.     SetControlMaximum((**theWindDocHndl).theVScrollBar,maxYsize);
  213.     SetControlMaximum((**theWindDocHndl).theHScrollBar,maxXsize);
  214.     SetControlValue((**theWindDocHndl).theVScrollBar,0);
  215.     SetControlValue((**theWindDocHndl).theHScrollBar,0);
  216.     ShowWindow((WindowPtr)theCWindow);
  217.     DrawGrowIcon((WindowPtr)theCWindow);
  218.     return error;
  219. }
  220.  
  221.  
  222. // Close an application window
  223.  
  224. #pragma segment windows
  225. void DisposeImageWindow (WindowPtr window)
  226. {
  227.     ImageDocHndl        theDocRefCon;
  228.     MenuHandle            theMenu;
  229.     WindowPtr            theWindow;
  230.     Str255                theString;
  231.     short                count;
  232.     
  233.     // check if the window to close is a valid Document window
  234.     if ((theDocRefCon=(ImageDocHndl)GetWRefCon(window)) != 0)
  235.     {
  236.         // dispose of the window document structure
  237.         if (!(**theDocRefCon).isColorsWindow)
  238.             DeleteDocNameFromMenu (theDocRefCon);
  239.         else
  240.         {
  241.             GetIndString (theString,rMenuItems,iShowColors);
  242.             SetMenuItemText (GetMHandle(mWindow),iColors,theString);
  243.             if ((**theDocRefCon).theColorsPalette)        // Direct color images have no palette
  244.                 DisposePalette((**theDocRefCon).theColorsPalette);
  245.         }
  246.         if ((**theDocRefCon).theVScrollBar)
  247.         {
  248.             DisposeControl((**theDocRefCon).theVScrollBar);
  249.             DisposeControl((**theDocRefCon).theHScrollBar);
  250.         }
  251.         /****    Dispose QuickDraw specific document items    ****/
  252.         if (!(**theDocRefCon).isUsingQDGX)
  253.         {
  254.             if ((**theDocRefCon).theImageWorld)
  255.                 DisposeGWorld((**theDocRefCon).theImageWorld);
  256.             if ((**theDocRefCon).hasUndoTemp)
  257.                 RemoveTempFile(theDocRefCon, true, false);
  258.             if ((**theDocRefCon).hasRedoTemp)
  259.                 RemoveTempFile(theDocRefCon, false, true);
  260.         }
  261.             
  262.         /****    Dispose of GX related objects    ****/
  263.         if ((**theDocRefCon).isUsingQDGX)
  264.         {
  265.             GXDisposeShape((**theDocRefCon).theGXImageShape);
  266.             GXDisposeInk((**theDocRefCon).theInkShape);
  267.             GXDisposeViewPort((**theDocRefCon).theGxChildView);
  268.             GXDisposeViewPort((**theDocRefCon).theGXview);
  269.         }
  270.         DisposeHandle((Handle)theDocRefCon);
  271.         DisposeWindow(window);
  272.         theWindow = FrontWindow();
  273.         if (theWindow == nil || gNumOpenWindows == 0)
  274.         {
  275.             if (ColorWindowVisible())
  276.                 DisposeColorsWindow();
  277.             theMenu = GetMHandle(mFile);
  278.             DisableItem(theMenu,iClose);
  279.             DisableItem(theMenu,iCloseAll);
  280.             DisableItem(theMenu,iSaveAs);
  281.             DisableItem(theMenu,iRevert);
  282.             DisableItem(GetMHandle(mWindow),iColors);
  283.             theMenu = GetMHandle(mFilters);
  284.             for (count = iSmoothFilter;count <= iHiPassFilter; ++count)
  285.                 DisableItem(theMenu,count);
  286.             theMenu = GetMHandle(mEffects);
  287.             for (count = iInvert;count <= iRotate180; ++count)
  288.                 DisableItem(theMenu,count);
  289.             /****    Reset undo menu item text    ****/
  290.             SetUndoItemText(kCannotUndo);
  291.             ForceRestoreColors();
  292.         }
  293.         else
  294.         {
  295.             theDocRefCon=(ImageDocHndl)GetWRefCon(theWindow);
  296.             switch ((*theDocRefCon)->theUndoState)
  297.             {
  298.                 case kCannotUndo:
  299.                     SetUndoItemText(kCannotUndo);
  300.                 break;
  301.                 case kCanUndo:
  302.                     SetUndoItemText(kCanUndo);
  303.                 break;
  304.                 case kCanRedo:
  305.                     SetUndoItemText(kCanRedo);
  306.                 break;
  307.             }
  308.         }
  309.     }
  310. }
  311.  
  312.  
  313. /*****    Perform update operation for image and colors windows for both imaging models    ****/
  314.  
  315. #pragma segment windows
  316. void    DoWindowUpdate(WindowPtr theWindow)
  317. {
  318.     PixMapHandle        thePixMapHndl = nil;
  319.     ImageDocHndl        theWindDocHndl;
  320.     WindowPtr            oldPort;
  321.     
  322.     GetPort(&oldPort);
  323.     SetPort(theWindow);
  324.     BeginUpdate(theWindow);
  325.     theWindDocHndl = (ImageDocHndl)GetWRefCon(theWindow);
  326.     if (theWindDocHndl && (**theWindDocHndl).isColorsWindow == false && 
  327.                           (**theWindDocHndl).isUsingQDGX == false)
  328.     {
  329.         TransferImageToWindow(theWindDocHndl,theWindow);    // Color QD content region update
  330.         UpdateControls(theWindow,theWindow->visRgn);
  331.         DrawGrowIcon(theWindow);
  332.     }
  333.     else if (theWindDocHndl && 
  334.             (**theWindDocHndl).isColorsWindow == false && 
  335.             (**theWindDocHndl).isUsingQDGX)
  336.     {
  337.         UpdateGXObjectDisplay(theWindDocHndl, theWindow);
  338.         UpdateControls(theWindow,theWindow->visRgn);
  339.         DrawGrowIcon(theWindow);
  340.     }
  341.     else if ((**theWindDocHndl).isColorsWindow && 
  342.              (**theWindDocHndl).isUsingQDGX == false)
  343.         DrawImageColors(theWindow);
  344.     EndUpdate(theWindow);
  345.     SetPort (oldPort);
  346. }
  347.  
  348.  
  349. /****    Create new Image Colors Window    ****/
  350.  
  351. #pragma segment windows
  352. OSErr    CreateColorsWindow(void)
  353. {
  354.     ImageDocHndl        theColorsDocHndl = nil,
  355.                         theFoundDocHndl = nil;
  356.     CWindowPtr            theWindow;
  357.     WindowPtr            theFrontWindow;
  358.     Str255                theString;
  359.     OSErr                error = noErr;
  360.                     
  361.     theWindow = (CGrafPort*)GetNewCWindow(rColorsWindow,nil,(WindowPtr)-1L);
  362.     theColorsDocHndl = (ImageDocHndl)NewHandle(sizeof(ImageDoc));    // alloc new image document
  363.     if (theColorsDocHndl)
  364.     {
  365.         SetWRefCon((WindowPtr)theWindow,(long)theColorsDocHndl);    // set refcon to doc handle
  366.         (**theColorsDocHndl).theImageWorld = nil;
  367.         (**theColorsDocHndl).theVScrollBar = nil;
  368.         (**theColorsDocHndl).theHScrollBar = nil;
  369.         (**theColorsDocHndl).isColorsWindow = true;
  370.         (**theColorsDocHndl).theColorsPalette = nil;        // initialise to nil as direct images have no palette info
  371.         (**theColorsDocHndl).isUsingQDGX = false;
  372.         (**theColorsDocHndl).theUndoState = kCannotUndo;
  373.         (**theColorsDocHndl).hasUndoTemp = false;
  374.         (**theColorsDocHndl).hasRedoTemp = false;
  375.         theFrontWindow = FrontWindow();
  376.         theFoundDocHndl = (ImageDocHndl)GetWRefCon(theFrontWindow);
  377.         (**theColorsDocHndl).theImageDepth = (**theFoundDocHndl).theImageDepth;
  378.         SetWTitle((WindowPtr)theWindow,"\pImage Colors");
  379.         SizeWindow((WindowPtr)theWindow,128+32,32+32,false);
  380.         ShowWindow((WindowPtr)theWindow);
  381.         if ((**theFoundDocHndl).theImageDepth <= 8)    // create palette for indexed images
  382.         {
  383.             if (!(**theFoundDocHndl).isUsingQDGX)
  384.                 (**theColorsDocHndl).theColorsPalette = 
  385.                     NewPalette(256,
  386.                                (**(*(**theFoundDocHndl).theImageWorld).portPixMap).pmTable,
  387.                                pmTolerant+pmExplicit,0);
  388.             else
  389.                 (**theColorsDocHndl).theColorsPalette = 
  390.                     NewPalette((**theFoundDocHndl).theImageDepth,
  391.                                nil,pmTolerant+pmExplicit,0);
  392.  
  393.             if ((**theColorsDocHndl).theColorsPalette)
  394.                  NSetPalette((WindowPtr)theWindow,(**theColorsDocHndl).theColorsPalette,pmAllUpdates);
  395.             else
  396.                 return kFailMakePalette;
  397.         }
  398.         GetIndString (theString,rMenuItems,iHideColors);
  399.         SetMenuItemText (GetMHandle(mWindow),iColors,theString);
  400.         SetUndoItemText((*theColorsDocHndl)->theUndoState);
  401.     }
  402.     else
  403.         error = kDefaultAppError;
  404.     return error;
  405. }
  406.  
  407.  
  408. /****    Dispose of Current image Colors window    ****/
  409.  
  410. #pragma segment windows
  411. OSErr    DisposeColorsWindow(void)
  412. {
  413.     ImageDocHndl    theDocHndl = nil;
  414.     WindowPtr        theWindow,
  415.                     theNextWindow;
  416.     OSErr            error = noErr;
  417.     
  418.     theWindow = FrontWindow();
  419.     while (theWindow)
  420.     {
  421.         theDocHndl = (ImageDocHndl)GetWRefCon(theWindow);
  422.         if ((**theDocHndl).isColorsWindow)
  423.         {
  424.             DisposeImageWindow (theWindow);
  425.             return error;
  426.         }
  427.         theNextWindow = &((WindowPeek)theWindow)->nextWindow->port;
  428.         theWindow = theNextWindow;
  429.     }
  430.     return kDefaultAppError;
  431. }
  432.  
  433.  
  434. /****    determine maximum window size    ****/
  435.  
  436. #pragma segment windows
  437. OSErr    CalcMaxImageWindowSize(ImageDocHndl theWindDocHndl)
  438. {
  439.     GDHandle        theMainGDevHndl;
  440.     Rect            maxWindowSize,
  441.                     theImageSize;
  442.     short            theMenuHeight;
  443.     OSErr            error = noErr;
  444.     
  445.     SetRect(&theImageSize,0,0,(**theWindDocHndl).theImageXSize+kScrollBarWidth,
  446.                               (**theWindDocHndl).theImageYSize+kScrollBarWidth);
  447.     theMainGDevHndl = GetMainDevice();
  448.     maxWindowSize = (**theMainGDevHndl).gdRect;
  449.     theMenuHeight = GetMBarHeight();
  450.     maxWindowSize.bottom -=theMenuHeight+20;
  451.     maxWindowSize.right -= 20;
  452.     if (((**theWindDocHndl).theImageXSize+kScrollBarWidth) < (maxWindowSize.right - maxWindowSize.left) &&
  453.         ((**theWindDocHndl).theImageYSize+kScrollBarWidth) < (maxWindowSize.bottom - maxWindowSize.top))
  454.     {
  455.         (**theWindDocHndl).theMaxWindowSize = theImageSize;        
  456.     }
  457.     else
  458.     {
  459.         /**** Set max window size depth and width to not larger than main screen or image    ****/
  460.         (**theWindDocHndl).theMaxWindowSize = maxWindowSize;
  461.         if (((**theWindDocHndl).theImageXSize+kScrollBarWidth) < (maxWindowSize.right - maxWindowSize.left))
  462.         {
  463.             (**theWindDocHndl).theMaxWindowSize.left = theImageSize.left;
  464.             (**theWindDocHndl).theMaxWindowSize.right = theImageSize.right;
  465.         }
  466.         if (((**theWindDocHndl).theImageYSize+kScrollBarWidth) < (maxWindowSize.bottom - maxWindowSize.top))
  467.         {
  468.             (**theWindDocHndl).theMaxWindowSize.top = theImageSize.top;
  469.             (**theWindDocHndl).theMaxWindowSize.bottom = theImageSize.bottom;
  470.         }
  471.     }
  472.     return error;
  473. }
  474.  
  475.  
  476. /****    position next open image window    staggered on main window to front most image window ****/
  477.  
  478. #pragma segment windows
  479. Point    CalcNextWindowPosition(ImageDocHndl theDocHndl)
  480. {
  481.     ImageDocHndl     theWindDocHndl;
  482.     WindowPtr        theWindow,
  483.                     theNextWindow,
  484.                     oldPort;
  485.     Point            theWindowOffset;
  486.     short            theMenuHeight;
  487.     
  488.     GetPort(&oldPort);
  489.     theMenuHeight = GetMBarHeight();
  490.     theWindow = FrontWindow();
  491.     if (!theWindow)
  492.     {
  493.         theWindowOffset.h = 0;
  494.         theWindowOffset.v = theMenuHeight+18;
  495.         return theWindowOffset;
  496.     }
  497.     theWindDocHndl = (ImageDocHndl)GetWRefCon(theWindow);
  498.     if (!(**theWindDocHndl).isColorsWindow)
  499.     {
  500.         SetPort(theWindow);
  501.         theWindowOffset.h = theWindow->portRect.left;
  502.         theWindowOffset.v = theWindow->portRect.top;
  503.         LocalToGlobal(&theWindowOffset);
  504.     }
  505.     else
  506.     {
  507.         
  508.         do {
  509.             theNextWindow = &((WindowPeek)theWindow)->nextWindow->port;
  510.             theWindow = theNextWindow;
  511.             theWindDocHndl = (ImageDocHndl)GetWRefCon(theWindow);
  512.         } while (theWindow && (**theWindDocHndl).isColorsWindow);
  513.         if (theWindow)
  514.         {
  515.             SetPort(theWindow);
  516.             theWindowOffset.h = theWindow->portRect.left;
  517.             theWindowOffset.v = theWindow->portRect.top;
  518.             LocalToGlobal(&theWindowOffset);
  519.         }
  520.         else
  521.         {
  522.             theWindowOffset.h = 0;
  523.             theWindowOffset.v = 0;
  524.         }
  525.     }
  526.     theWindowOffset.h +=18;
  527.     theWindowOffset.v +=18;
  528.     TestWindowFitMainScrn(theDocHndl, &theWindowOffset);
  529.     SetPort(oldPort);
  530.     return theWindowOffset;
  531. }
  532.  
  533.  
  534. /****    Test for window fit main screen    ****/
  535.  
  536. #pragma segment windows
  537. void    TestWindowFitMainScrn(ImageDocHndl theDocHndl, Point *windowOffset)
  538. {
  539.     GDHandle        theMainGDevHndl;
  540.     Rect            mainScrnSize,
  541.                     *maxWindowSize;
  542.     Point            theWindowOffset;
  543.     short            theMenuHeight;
  544.  
  545.     theMainGDevHndl = GetMainDevice();
  546.     mainScrnSize = (**theMainGDevHndl).gdRect;
  547.     theMenuHeight = GetMBarHeight();
  548.     mainScrnSize.bottom -=theMenuHeight+20;
  549.     maxWindowSize = &(*theDocHndl)->theMaxWindowSize;
  550.     if ((maxWindowSize->right+windowOffset->h) > mainScrnSize.right)
  551.     {
  552.         windowOffset->h = 0;
  553.         if ((maxWindowSize->right+windowOffset->h) > mainScrnSize.right)
  554.             maxWindowSize->right = mainScrnSize.right - 20;
  555.     }
  556.     if ((maxWindowSize->bottom+windowOffset->v) > mainScrnSize.bottom)
  557.     {
  558.         windowOffset->v = theMenuHeight+18;
  559.         if ((maxWindowSize->bottom+windowOffset->v) > mainScrnSize.bottom)
  560.             maxWindowSize->bottom = mainScrnSize.bottom;
  561.     } 
  562. }
  563.  
  564.  
  565. /****    Resize window    ****/
  566.  
  567. #pragma segment windows
  568. void    HandleWindowReSize(WindowPtr theWindow,Point startPoint)
  569. {
  570.     ImageDocHndl        theWindDocHndl;
  571.     WindowPtr            oldPort;
  572.     long                returnCoord;
  573.     Rect                resizeBounds;
  574.     short                windWidth,
  575.                         windHeight,
  576.                         maxXsize,
  577.                         maxYsize;
  578.     
  579.     GetPort(&oldPort);
  580.     theWindDocHndl = (ImageDocHndl)GetWRefCon((WindowPtr)theWindow);
  581.     SetRect(&resizeBounds,96,96,
  582.             (1+(**theWindDocHndl).theMaxWindowSize.right - (**theWindDocHndl).theMaxWindowSize.left),
  583.             (1+(**theWindDocHndl).theMaxWindowSize.bottom - (**theWindDocHndl).theMaxWindowSize.top));
  584.     returnCoord = GrowWindow(theWindow, startPoint, &resizeBounds);
  585.     windHeight   = HiWord(returnCoord);
  586.     windWidth = LoWord(returnCoord);
  587.     SizeWindow(theWindow, windWidth, windHeight, true);
  588.     MoveControl((**theWindDocHndl).theVScrollBar,
  589.                 theWindow->portRect.right - kScrollBarWidth,theWindow->portRect.top - 1);
  590.     MoveControl((**theWindDocHndl).theHScrollBar,
  591.                 theWindow->portRect.left - 1,theWindow->portRect.bottom-kScrollBarWidth);
  592.     SizeControl((**theWindDocHndl).theVScrollBar,16,
  593.                 (theWindow->portRect.bottom - theWindow->portRect.top)-13);
  594.     SizeControl((**theWindDocHndl).theHScrollBar,
  595.                 (theWindow->portRect.right - theWindow->portRect.left)-13,16);
  596.     maxXsize = (*theWindDocHndl)->theImageWorld->portRect.right - 
  597.                 ((theWindow->portRect.right - kScrollBarWidth) - theWindow->portRect.left);
  598.     maxYsize = (*theWindDocHndl)->theImageWorld->portRect.bottom - 
  599.                 ((theWindow->portRect.bottom - kScrollBarWidth) - theWindow->portRect.top);
  600.     SetControlMaximum((**theWindDocHndl).theVScrollBar,maxYsize);
  601.     SetControlMaximum((**theWindDocHndl).theHScrollBar,maxXsize);
  602.     SetPort(theWindow);
  603.     InvalRect(&theWindow->portRect);
  604.     SetPort(oldPort);
  605. }
  606.  
  607.  
  608. /****    Display Progres Bar Dialog    *****/
  609.  
  610. #pragma segment windows
  611. void    DisplayProgressBarDlog(short updateState, short controlValue, short theStrGroup, short theStrID)
  612. {
  613.     static ControlHandle    theControl;
  614.     static GrafPtr            oldPort;
  615.     static CWindowPtr        theProgressWindow;
  616.     Str255                    theProgressStr;
  617.                     
  618.     GetPort(&oldPort);
  619.     // Perform operations on dialog and progress control dependent on updateState parameter
  620.     switch (updateState)
  621.     {
  622.          case kDisplayProgressWindow:
  623.             DisableAllMenus(true);
  624.             theProgressWindow = (CWindowPtr) GetNewCWindow(rProgressBarWindow,nil,((WindowPtr)-1L));
  625.             SetPort    ((WindowPtr)theProgressWindow);
  626.             theControl=GetNewControl(rProgressCntl, (WindowPtr)theProgressWindow);
  627.             MoveControl(theControl,20,32);
  628.             ShowControl(theControl);
  629.             GetIndString (theProgressStr,theStrGroup,theStrID);
  630.             if (theStrGroup != 0)
  631.             {
  632.                 MoveTo(20,64);
  633.                 DrawString(theProgressStr);
  634.             }
  635.         break;
  636.         case kUpdateProgressWindow:
  637.             SetPort    ((WindowPtr)theProgressWindow);
  638.             SetControlValue(theControl,controlValue);
  639.         break;
  640.         case kDisposeProgressWindow:
  641.             DisposeControl(theControl);
  642.             DisposeWindow((WindowPtr)theProgressWindow);
  643.             DisableAllMenus(false);
  644.         break;
  645.     }
  646.     SetPort(oldPort);
  647. }
  648.  
  649.  
  650. /****    Display Startup splash screen    *****/
  651.  
  652. #pragma segment windows
  653. void SplashScreen(short theDelayLength)
  654. {
  655.     WindowPtr        theWindow,
  656.                     oldPort;
  657.     PicHandle        thePict = nil;
  658.     signed long        currentTime,
  659.                     delayTime;
  660.     
  661.     GetPort(&oldPort);
  662.     thePict=GetPicture(rSplashPICT);
  663.     if (thePict==nil)
  664.         return;        
  665.     theWindow=GetNewWindow(rSplashWindow,nil,(WindowPtr)-1L);
  666.     SetPort(theWindow);
  667.     HLock((Handle)thePict);
  668.     DrawPicture(thePict,&theWindow->portRect);
  669.     HUnlock((Handle)thePict);
  670.     DisposeHandle((Handle)thePict);
  671.     currentTime=LMGetTicks();
  672.     delayTime = currentTime+theDelayLength;
  673.     do
  674.     {
  675.         currentTime = LMGetTicks();
  676.     }while (currentTime < delayTime);
  677.     DisposeWindow((GrafPtr)theWindow);
  678.     SetPort(oldPort);
  679. }
  680.  
  681.  
  682. /****    Test for open colors window        ****/
  683.  
  684. #pragma segment windows
  685. Boolean    ColorWindowVisible(void)
  686. {
  687.     Str255        theItemString,
  688.                 theResString;
  689.     Boolean        isVisible = false;
  690.  
  691.     GetMenuItemText(GetMHandle(mWindow),iColors,theItemString);    
  692.     GetIndString (theResString,rMenuItems,iHideColors);
  693.     if (EqualString(theItemString,theResString,true,true))
  694.         isVisible = true;
  695.     return isVisible;
  696. }
  697.